package com.jarvis.app.car;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.jarvis.app.car.adapter.AnswerTicketAdapter;
import com.jarvis.app.car.common.MessageResult;
import com.jarvis.app.car.constant.AppConstant;
import com.jarvis.app.car.model.Car;
import com.jarvis.app.car.model.Ticket;
import com.jarvis.app.car.util.ServiceManager;
import com.jarvis.app.car.util.ThreadPoolManager;

import java.util.List;

public class AnswerTicketActivity extends AppCompatActivity {
    private ImageView iv_back;

    private TextView tv_addTicket;

    private TextView tv_titleName;

    private RecyclerView ticketRecyclerView;

    private AnswerTicketAdapter answerTicketAdapter;

    private final Handler handler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(@NonNull Message message) {
            MessageResult<List<Ticket>> messageResult = (MessageResult<List<Ticket>>) message.obj;
            if (!messageResult.isJudgeSuccess()) {

            } else {
                List<Ticket> result = messageResult.getResult();
                answerTicketAdapter.setTickets(result);
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_answer_ticket);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        iv_back = findViewById(R.id.iv_back);
        tv_addTicket = findViewById(R.id.tv_addTicket);
        tv_titleName = findViewById(R.id.tv_titleName);
        ticketRecyclerView = findViewById(R.id.ticketRecyclerView);
        answerTicketAdapter = new AnswerTicketAdapter();
        ticketRecyclerView.setAdapter(answerTicketAdapter);


        answerTicketAdapter.setItemViewOnClickListener(new AnswerTicketAdapter.ItemViewOnClickListener() {
            @Override
            public void infoOnClick(Ticket ticket, int position) {
                Intent intent = new Intent(AnswerTicketActivity.this, TicketActivity.class);
                intent.putExtra("ticketId", ticket.getId());
                startActivityForResult(intent, 1);
            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        System.out.println("ACCOUNT_SUPER = " + AppConstant.ACCOUNT_SUPER);
        if (AppConstant.ACCOUNT_SUPER == 1) {
            tv_addTicket.setVisibility(View.GONE);
            tv_titleName.setText("待处理工单");

        } else {
            tv_addTicket.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(AnswerTicketActivity.this, AddTicketActivity.class);

                    startActivityForResult(intent, 1);
                }
            });
        }

        loadData();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Integer.MIN_VALUE) {
            loadData();
        }
    }


    public void loadData() {
        ThreadPoolManager.threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {

                MessageResult<List<Ticket>> listMessageResult = null;
                if (AppConstant.ACCOUNT_SUPER == 1) {
                    listMessageResult = ServiceManager.ticketService.listNoAnswer();
                } else {
                    listMessageResult = ServiceManager.ticketService.listByUserId(AppConstant.ACCOUNT_ID);
                }

                Message msg = Message.obtain();
                msg.obj = listMessageResult;
                handler.sendMessage(msg);

            }
        });
    }
}